﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using XL.Crawler.Core.Crawlers.CrawlerEventArgs;

namespace XL.Crawler.Core.Crawlers
{
    /// <summary>
    /// 爬重基类
    /// </summary>
    public abstract class CrawlerBase : ICrawler
    {
        /// <summary>
        /// 开始事件
        /// </summary>
        public event CrawlerEvent<StartEventArgs>? OnStartEvent;
        /// <summary>
        /// 结束事件
        /// </summary>
        public event CrawlerEvent<CompletedEventArgs>? OnCompletedEvent;
        /// <summary>
        /// 异常事件
        /// </summary>
        public event CrawlerEvent<ExceptionEventArgs>? OnExceptionEvent;

        /// <summary>
        /// 请求工厂
        /// </summary>
        private readonly Lazy<IHttpClientFactory> _httpClientFactory;
        /// <summary>
        /// 请求工厂
        /// </summary>
        protected internal IHttpClientFactory HttpClientFactory => _httpClientFactory.Value;
        /// <summary>
        /// 配置
        /// </summary>
        protected internal CrawlerOptions CrawlerOptions => _crawlerOptions.Value;

        /// <summary>
        /// 配置
        /// </summary>
        private readonly Lazy<CrawlerOptions> _crawlerOptions;
        /// <summary>
        /// 服务容器提供者
        /// </summary>
        private readonly IServiceProvider _serviceProvider;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="serviceProvider"></param>
        public CrawlerBase(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _crawlerOptions = new Lazy<CrawlerOptions>(() =>
            {
                return _serviceProvider.GetRequiredService<IOptions<CrawlerOptions>>().Value;
            });
            _httpClientFactory = new Lazy<IHttpClientFactory>(() =>
             {
                 return _serviceProvider.GetRequiredService<IHttpClientFactory>();
             });
        }

        /// <summary>
        /// 开始爬虫
        /// </summary>
        /// <returns></returns>
        public abstract Task StartAsync();

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        protected virtual void OnStart(object obj, StartEventArgs args)
        {
            OnStartEvent?.Invoke(obj, args);
        }
        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        protected virtual void OnException(object obj, ExceptionEventArgs args)
        {
            OnExceptionEvent?.Invoke(obj, args);
        }
        /// <summary>
        /// 完成
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        protected virtual void OnCompleted(object obj, CompletedEventArgs args)
        {
            OnCompletedEvent?.Invoke(obj, args);
        }


    }
}
